/**
 * 
 */
package com.ekingstar.framework.action;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.config.Action;
import org.apache.struts2.config.Result;
import org.apache.struts2.config.Results;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.conversion.annotations.Conversion;
import com.ekingstar.framework.domain.User;
import com.ekingstar.framework.security.UserUtil;
import com.ekingstar.framework.service.ISystemLogService;
import com.ekingstar.framework.service.IUserService;

/**
 * @author GQ
 *
 */

@SuppressWarnings({"serial" })
@Action(name="sjyz",namespace="/")
@Results( {
		//@Result(name = "success", value = "/user/success.jsp"),
		@Result(name = "error", value = "/error.jsp"),
		@Result(name="info",value="/edu/info.jsp")
		//@Result(name = "input", value = "userinfo", type = ActionChainResult.class, params = {
		//		"namespace", "/user", "method", "input" })
})
@Conversion
public class UserSjyzAction extends ActionSupport{
	
	private IUserService userService;
	private ISystemLogService systemLogService;
	//private I
	private String sjyzm;
	private String s_sjyzm;
	
	
	public String execute() {
		User user = UserUtil.getCurrentUser();
		HttpServletRequest request  = ServletActionContext.getRequest();
		HttpServletResponse response = ServletActionContext.getResponse();
		HttpSession session = request.getSession();
		
		Object obj = session.getAttribute("isyz");
		String isyz = "";
		if(this.systemLogService.isUserLastLoginIp(user.getId())){
			session.setAttribute("isyz", "1");
			try {
				response.sendRedirect(request.getContextPath());
				return null;
			} catch (IOException e) {
				request.setAttribute("info", "系统错误请与管理员联系！");
				e.printStackTrace();
				return "info";
				
			}
		}
		
		/*TUserMobileSjyz sjyzxx = new TUserMobileSjyz();
		sjyzxx.setUserid(user.getId());
		sjyzxx.setSjyzm(this.sjyzm );
		sjyzxx.setFsnr("登陆系统的验证为："+this.sjyzm);
		sjyzxx.setFssj(new Date());
		sjyzxx.setJshm(user.getUserInfo().getMobile());*/
		
		try{
		}catch(RuntimeException e){
			request.setAttribute("info", e.getMessage());
			request.setAttribute("ljname","更新个人信息");
			request.setAttribute("ljdz",request.getContextPath()+"/user/userinfo!input.action");
			//e.printStackTrace();
			return "info";
		}
		
		return SUCCESS;
	}
	
	public String sub(){
		if(!sjyzm.trim().equals(s_sjyzm.trim())){
			this.addFieldError(ERROR, "验证码不正确！");
			return SUCCESS;
		}else{
			HttpServletRequest request  = ServletActionContext.getRequest();
			HttpServletResponse response = ServletActionContext.getResponse();
			HttpSession session = request.getSession();
			session.setAttribute("isyz", "1");
			try {
				response.sendRedirect(request.getContextPath()+"default.jsp");
			} catch (IOException e) {
				request.setAttribute("info", "系统错误请与管理员联系！");
				e.printStackTrace();
			}
		}
		return null;
	}
	public IUserService getUserService() {
		return userService;
	}
	public void setUserService(IUserService userService) {
		this.userService = userService;
	}
	public String getSjyzm() {
		return sjyzm;
	}
	public void setSjyzm(String sjyzm) {
		this.sjyzm = sjyzm;
	}

	public String getS_sjyzm() {
		return s_sjyzm;
	}

	public void setS_sjyzm(String s_sjyzm) {
		this.s_sjyzm = s_sjyzm;
	}


	public ISystemLogService getSystemLogService() {
		return systemLogService;
	}

	public void setSystemLogService(ISystemLogService systemLogService) {
		this.systemLogService = systemLogService;
	}
}
